<%@ page import = "ro.gateway.aida.obj.*,
                   ro.gateway.aida.utils.Utils,
                   ro.gateway.aida.db.PersistenceToken,
                   ro.gateway.aida.db.PersistenceManager" %>
<%@ page import = "ro.gateway.aida.obj.db.*" %>
<%@ page import = "java.sql.SQLException" %>
<%
      PersistenceToken token = PersistenceManager.tokenLookup( application );
        // 1 - region
        // 2 - County
        // 3 - oras
        // 4 - localitate
        // 0 - tara
        int detail_level = Utils.getInt(request, "level", 4);
        if (detail_level < 1) detail_level = 1;
        if (detail_level > 4) detail_level = 4;

        int region_id = Utils.getInt(request, "rid", -1);
        int County_id = Utils.getInt(request, "jid", -1);
        int oras_id = Utils.getInt(request, "oid", -1);
        int localitate_id = Utils.getInt(request, "lid", -1);
        // 1 - regiune, 2 - County, 3 - oras
        int field_modified = Utils.getInt(request, "fmdf", 0);
        if (field_modified == 1) {County_id=-1; oras_id=-1; localitate_id=-1;}
        if (field_modified == 2) {oras_id=-1; localitate_id=-1;}
        if (field_modified == 3) {localitate_id=-1;}

        String key = Utils.getValidTrimedString(request, "key", null);
        if (key == null) return;

      Region[] regiuni = new Region[0];
      County[] Counties = new County[0];
      Town[] orase = new Town[0];
      Village[] localitati = new Village[0];
      try {
            while (true) {
                  regiuni = RegionDB.getManager(token ).getAll();
                  if (detail_level >= 2) {
                        Counties = CountyDB.getManager(token ).getForRegion(region_id);
                        if (Counties == null) {
                              region_id = regiuni[0].getId();
                        }
                        Counties = CountyDB.getManager(token ).getForRegion(region_id);
                        if (Counties == null) {
                              Counties = new County[0];
                        }
                  } else break;

                  if (detail_level >= 3) {
                        if (County_id == -1) {
                              County_id = Counties[0].getId();
                        }
                        orase = TownDB.getManager(token ).getAllForCounty(County_id);
                        if (orase == null) {
                              orase = new Town[0];
                        }
                  } else break;

                  if (detail_level >= 4) {
                        if (oras_id == -1) oras_id = orase[0].getSirsup_id();
                        localitati = VillageDB.getManager(token ).getAllForTown(oras_id);
                        if (localitati == null) {
                              localitati = new Village[0];
                        }
                  } else break;

                  break;
            }
      } catch (SQLException sqlEx) {
            sqlEx.printStackTrace();
      }
      %>
<html>
<head>
<title>Romania Development Gateway - AIDA - Location selector</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="alf.css" type="text/css">
</head>

<body bgcolor="#FFFFFF" text="#000000">
        <table width="90%" border="0" cellspacing="1" cellpadding="5" align="center" bgcolor="#CCCCCC">
		<form method="POST" name="theform">
		<input type="hidden" name="level" value="<%=detail_level%>" />
		<input type="hidden" name="fmdf" value="" />
		<input type="hidden" name="key" value="<%=key%>" />
		<tr bgcolor="f9f9f9"> 
			<td colspan="2" width="39%" class="text_search"><b>Selectati locatia</b></td>
		</tr>
		<% if (detail_level >= 1) { %>
		<tr bgcolor="f9f9f9">
			<td class="text_search" width="20%">Regiunea</td>
			<td class="text_search" width="80%">
				<select name="rid" <%=(detail_level > 1) ? "onChange=\"javascript:theform.fmdf.value=1;theform.submit();\"":""%>>
				<% for (int i = 0; i < regiuni.length; i++) { %>
				<option value="<%=regiuni[i].getId()%>" <%=(regiuni[i].getId() == region_id) ? "selected=\"true\"" : ""%>><%=regiuni[i].getName()%></option>
				<% } %>
				</select>
			</td>
		</tr>
		<% } %>
		<% if (detail_level >= 2) { %>
		<tr bgcolor="f9f9f9">
			<td class="text_search">Countyul</td>
			<td class="text_search">
				<select name="jid" <%=(detail_level > 2) ? "onChange=\"javascript:theform.fmdf.value=2;theform.submit();\"":""%>>
				<% for (int i = 0; i < Counties.length; i++) { %>
				<option value="<%=Counties[i].getId()%>" <%=(Counties[i].getId() == County_id) ? "selected=\"true\"" : ""%>><%=Counties[i].getName()%></option>
				<% } %>
				</select>
			</td>
		</tr>
		<% } %>
		<% if (detail_level >= 3) { %>
		<tr bgcolor="f9f9f9">
			<td class="text_search">Municipiu<br>/Town<br>/Comuna</td>
			<td class="text_search">
				<select name="oid" <%=(detail_level > 3) ? "onChange=\"javascript:theform.fmdf.value=3;theform.submit();\"":""%>>
				<% for (int i = 0; i < orase.length; i++) { %>
				<option value="<%=orase[i].getSirsup_id()%>" <%=(orase[i].getSirsup_id() == oras_id) ? "selected=\"true\"" : ""%>><%=orase[i].getName()%> - <%=(orase[i].getType() == 1) ? "Municipiu " : ((orase[i].getType() == 2) ? "Town " : "Comuna ")%></option>
				<% } %>
				</select>
			</td>
		</tr>
		<% } %>
		<% if (detail_level >= 4) { %>
		<tr bgcolor="f9f9f9">
			<td class="text_search">Village</td>
			<td class="text_search">
				<select name="lid">
				<% for (int i = 0; i < localitati.length; i++) { %>
				<option value="<%=localitati[i].getSiruta_id()%>" <%=(localitati[i].getSiruta_id() == localitate_id) ? "selected=\"true\"" : ""%>><%=localitati[i].getName()%></option>
				<% } %>
				</select>
			</td>
		</tr>
		<% } %>
		</form>
		<script language="javascript">
		function go_back() {
			var url = "editActivity?key=" + document.theform.key.value + "&page=2" +
			"&action=add_loc&rid=" + document.theform.rid.options[document.theform.rid.selectedIndex].value;
			if (document.theform.jid != null) {
				url += "&jid=" + document.theform.jid.options[document.theform.jid.selectedIndex].value;
			}
			if (document.theform.oid != null) {
				url += "&oid=" + document.theform.oid.options[document.theform.oid.selectedIndex].value;
			}
			if (document.theform.lid != null) {
				url += "&lid=" + document.theform.lid.options[document.theform.lid.selectedIndex].value;
			}
			opener.location = url;
			window.close();
		}
		</script>
		<tr bgcolor="f9f9f9">
			<td class="text_search" colspan="2" align="center">
			<input type="button" value="Selecteaza" onclick="javascript:go_back();" >
			<input type="button" onclick="javascript:window.close();" value="Renunta">
			</td>
		</tr>
	</table>
</body>
</html>